pip install pyads
import pyads
AMSNETID = "10.123.67.6.1.1"
plc = pyads.Connection(AMSNETID, pyads.PORT_TC3PLC1)
plc.open()
print(f"Connected?: {plc.is_open}") #debugging statement, optional
print(f"Local Address? : {plc.get_local_address()}") #debugging statement, optional
import pyads
AMSNETID = "10.123.67.6.1.1"
plc = pyads.Connection(AMSNETID, pyads.PORT_TC3PLC1)
plc.open()
print(f"Connected?: {plc.is_open}")
print(f"Local Address? : {plc.get_local_address()}")
print(plc.read_state())
{attribute 'qualified_only'}
VAR_GLOBAL
bOut1: BOOL;
bOut2: BOOL;
END_VAR
PROGRAM MAIN
VAR
out1: BOOL;
out2: BOOL;
bDummy : BOOL;
END_VAR
out1 := GVL.bOut1;
out2 := GVL.bOut2;
var = plc.read_by_name("NAMEOFVARIABLE")
bOut1 = plc.read_by_name("GVL.bOut1")
print(f"bOut1 State {bOut1}")
plc.close()
import pyads
AMSNETID = "10.123.67.6.1.1"
plc = pyads.Connection(AMSNETID, pyads.PORT_TC3PLC1)
plc.open()
print(f"Connected?: {plc.is_open}")
print(f"Local Address? : {plc.get_local_address()}")
print(plc.read_state())
bOut1 = plc.read_by_name("GVL.bOut1")
print(f"bOut1 State {bOut1}")
plc.close()
plc.write_by_name("VARIABLENAME", REPLACEMENTVALUE)
plc.write_by_name("GVL.bOut1", True) #overrride values
bOut1 = plc.read_by_name("GVL.bOUt1") #reads to confirm
print(f"Final State {bOut1}")
import pyads
AMSNETID = "10.123.67.6.1.1"
plc = pyads.Connection(AMSNETID, pyads.PORT_TC3PLC1)
plc.open()
print(f"Connected?: {plc.is_open}")
print(f"Local Address? : {plc.get_local_address()}")
print(plc.read_state())
bOut1 = plc.read_by_name("GVL.bOut1")
print(f"bOut1 State {bOut1}")
plc.write_by_name("GVL.bOut1", True)
bOut1 = plc.read_by_name("GVL.bOUt1")
print(f"Final State {bOut1}")
plc.close()
iNumber: INT;
sWord: STRING;
PROGRAM MAIN
VAR
out1: BOOL;
out2: BOOL;
bDummy : BOOL;
iNumber: INT;
sWord: STRING;
END_VAR
def readAndWrite(plcInput, variable, state):
print(f"For variable {variable}:")
print("before writing: ",plcInput.read_by_name(variable))
plcInput.write_by_name(variable, state)
print("after writing: ",plcInput.read_by_name(variable), '\n')
import pyads
AMSNETID = "10.123.67.6.1.1" #change to what you need
plc = pyads.Connection(AMSNETID, pyads.PORT_TC3PLC1)
plc.open()
print(f"Connected?: {plc.is_open}")
print(f"Local Address? : {plc.get_local_address()}")
print(plc.read_state())
def readAndWrite(plcInput, variable, state):
print(f"For variable {variable}:")
print("before writing: ",plcInput.read_by_name(variable))
plcInput.write_by_name(variable, state)
print("after writing: ",plcInput.read_by_name(variable), '\n')
readAndWrite(plcInput = plc, variable = "MAIN.bDummy", state = True)
readAndWrite(plcInput = plc, variable = "MAIN.iNumber", state = 69)
readAndWrite(plcInput = plc, variable = "MAIN.sWord", state = "Hello World!")
readAndWrite(plcInput = plc, variable = "GVL.bOut1", state = True)
readAndWrite(plcInput = plc, variable = "GVL.bOut2", state = True)
plc.close()
C:\Users\MaxwellA.BECKHOFF\PycharmProjects\FirstPorject\venv\Scripts\python.exe C:/Users/MaxwellA.BECKHOFF/PycharmProjects/FirstPorject/ADSfirstTime.py
Connected?: True
Local Address? : <AmsAddress 10.123.67.6.1.1:32929>
(5, 0)
For variable MAIN.bDummy:
before writing: False
after writing: True
For variable MAIN.iNumber:
before writing: 0
after writing: 69
For variable MAIN.sWord:
before writing:
after writing: Hello World!
For variable GVL.bOut1:
before writing: True
after writing: True
For variable GVL.bOut2:
before writing: False
after writing: True
Process finished with exit code 0